Description
Implement
Customer.getMostExpensiveDeliveredProduct()
andShop.getNumberOfTimesProductWasOrdered()
using functions from the Kotlin standard library.
Kotlin標準ライブラリの関数を使用して、
Customer.getMostExpensiveDeliveredProduct()
およびShop.getNumberOfTimesProductWasOrdered()
を実装してください。
Code
// Return the most expensive product among all delivered products
// (use the Order.isDelivered flag)
fun Customer.getMostExpensiveDeliveredProduct(): Product? {
return orders.filter { it.isDelivered }.flatMap { it.products }.maxBy { it.price }
}
// Return how many times the given product was ordered.
// Note: a customer may order the same product for several times.
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int {
return customers.flatMap { it.getOrderedProductsList() }.count { it == product }
}
fun Customer.getOrderedProductsList(): List<Product> {
return orders.flatMap { it.products }
}
Memo
- これまでのコレクション関連の総おさらい問題
- Kotlin Koans 20 Collections Introduction
- Kotlin Koans 21 Filter; map
- Kotlin Koans 22 All, Any and other predicates
- Kotlin Koans 23 FlatMap
- Kotlin Koans 24 Max; min
- Kotlin Koans 25 Sort
- Kotlin Koans 26 Sum
- Kotlin Koans 27 Group By
- Kotlin Koans 28 Partition
- Kotlin Koans 29 Fold